package com.Blog.www.dao.Impl;

import com.Blog.www.dao.LabelDao;
import com.Blog.www.po.Label;
import com.Blog.www.utils.SqlDao;

import java.util.List;

/**
 * 博主主页的标签添加类
 *
 * @author 君未洋
 */
public class LabelDaoImpl implements LabelDao {
    private static SqlDao dao = new SqlDao();

    @Override
    public List<Label> getLabelList(Integer id, String labelPrior) {
        String sql = "select * from label where user_id = ? and prior = ?";
        List<Label> labels = dao.selectAllLabel(sql, id, labelPrior);
        if (labels.size() == 0) {
            return null;
        } else {
            return labels;
        }
    }

    @Override
    public Integer addLabel(int id, String labelPrior, String newLabel) {
        String sql = "insert into label values(null,?,?,?)";
        if (dao.update(sql, id, newLabel, labelPrior)) {
            sql = "select * from label where user_id = ? and content = ?";
            List<Label> labels = dao.selectAllLabel(sql, id, newLabel);
            if (labels != null && labels.size() != 0) {
                return labels.get(0).getId();
            }
        }
        return null;
    }

    @Override
    public Boolean updateLabel(String bid, int labelId) {
        String sql = "insert into blog_label values(null, ? , ? )";
        return dao.update(sql, bid, labelId);
    }

    @Override
    public List<String> getBlogLabelList(String labelPrior) {
        String sql = "select blog_id as bid from blog_label where label_id = ?";
        return dao.selectAllLabelBid(sql, labelPrior);
    }
}
